<template>
  <a-layout>
    <div class="head">
      <a-button icon="left" type="link" style="color: #d8d8d8" @click="$router.go(-1)">返回</a-button>
    </div>
    <div class="operation">
      <p style="font-size: 14px; color: #222426; margin-left: 20px">批量导入/修改数据</p>
      <div>
        <a-button @click="handleCancel"> 放弃编辑 </a-button>
        <a-button type="primary" class="save" @click="handleSave"> 保存 </a-button>
      </div>
    </div>
    <div class="tableBox">
      <a-table :columns="columns" :pagination="false" row-key="id" :data-source="dataList" :scroll="{ x: 1500 }">
        <div :slot="`${item.dataIndex}`" slot-scope="text, record" v-for="(item, index) in columns" :key="index">
          <span v-if="item.fieldGroup !== 'wage'">{{ record[`${item.dataIndex}`] }}</span>
          <a-input-number
            v-else-if="item === '出勤天数'"
            :min="0"
            :max="maxDate"
            :step="0.01"
            :precision="2"
            :value="record[`${item.dataIndex}`]"
            @change="handleSelfChange($event, record.id, item.dataIndex)"
          />
          <a-input-number
            v-else-if="item.dataIndex === '工时'"
            :min="0"
            :max="9999"
            :step="0.0001"
            :precision="4"
            :value="record[`${item.dataIndex}`]"
            @change="handleSelfChange($event, record.id, item.dataIndex)"
          />
          <a-input-number v-else :value="record[`${item.dataIndex}`]" @change="handleSelfChange($event, record.id, item.dataIndex)" />
        </div>
      </a-table>
    </div>
  </a-layout>
</template>
<script>
import { onlineEditNewReq } from "@/api/wages/monthly";
export default {
  data() {
    return {
      maxDate: 30,
      id: null,
      visible: false,
      companyId: this.$store.state.hr.companyId,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      columns: [],
      selectedRow: [],
      dataList: []
    };
  },

  created() {
    this.dataList = JSON.parse(sessionStorage.getItem("selectedRow"));
    const columnsTemp = JSON.parse(sessionStorage.getItem("salaryColumns"));
    this.columns = columnsTemp.map((item) => ({
      ...item,
      scopedSlots: { customRender: item.dataIndex }
    }));
    this.id = this.$route.query.id;
    this.maxDate = this.$moment(this.$route.query.date, "YYYY-MM").daysInMonth();
  },

  beforeDestory() {},
  methods: {
    limitNumber(value) {
      if (typeof value === "string") {
        return !isNaN(Number(value)) ? value.replace(/\./g, "") : 0;
      } else if (typeof value === "number") {
        return !isNaN(value) ? String(value).replace(/\./g, "") : 0;
      } else {
        return 0;
      }
    },
    handleCancel() {
      this.$router.go(-1);
    },
    handleSelfChange($event, id, index) {
      const temp = JSON.parse(JSON.stringify(this.dataList));
      const index2 = temp.findIndex((item) => item.id === id);
      temp[index2][`${index}`] = $event;
      this.dataList = JSON.parse(JSON.stringify(temp));
    },
    handleSave() {
      var arr = [];
      this.dataList.forEach((item2, index2) => {
        var obj = {};
        this.columns.forEach((item, index) => {
          if (item.fieldGroup !== "covariance") {
            obj[`${item.title}`] = item2[`${item.dataIndex}`];
          }
        });
        arr.push(obj);
      });
      onlineEditNewReq({
        nameContentMapList: arr,
        companyId: this.companyId,
        monthly: this.dataList[0].monthly,
        yearly: this.dataList[0].yearly,
        salaryTemplateSalaryId: this.dataList[0].salaryTemplateSalaryId
      })
        .then((res) => {
          this.$router.go(-1);
          this.$message.success("保存成功");
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    },
    showEdit() {
      this.visible = true;
    }
  }
};
</script>
<style lang="scss" scoped>
.operation {
  display: flex;
  justify-content: space-between;
  .save {
    margin-right: 20px;
    margin-left: 15px;
  }
}
.head {
  padding: 10px 0 10px 0;
  width: 100%;
  display: flex;
  text-align: center;
  line-height: 60px;
  justify-content: space-between;
  height: 60px;
  .title {
    margin-right: 47%;
    color: #333333;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 600;
    margin-top: -10px;
  }
}

.tableBox {
  padding: 20px;
}
.emp-title {
  padding-left: 24px;
  line-height: 48px;
  height: 48px;
  background: rgba(250, 250, 250, 1);
}
.submitBox {
  position: relative;
  right: 0;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e9e9e9;
  padding: 10px 16px;
  background: #fff;
  text-align: right;
  z-index: 1;
}
</style>
